using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc;
using Soft1ApiDemo.Domain.Entity;
using Soft1ApiDemo.Domain.Repository;
using Soft1ApiDemo.Infrastructure.Utils;

namespace Soft1ApiDemo.Api
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController:ControllerBase
    {
        private readonly ILogger<UsersController> _logger;
        private readonly IRepository<Users> _userRes;
        public UsersController(IRepository<Users> userRes,ILogger<UsersController> logger)
        {
            _userRes=userRes;
            _logger=logger;
        }

        public IEnumerable<dynamic> Index()
        {
            // _userRes.Add(new Users{
            //     Username="8882131238",
            //     Password="999900",
            // });
            // var id=Guid.NewGuid();
            // var xx=_userRes.Table.Where(x=>x.IsDeleted==false).ToArray();
            // return xx;

            // var list=_userRes.Table.ToList();
            // return list;

            var list=_userRes.Table.Select(x=>new{x.Id,x.Username,x.Password}).ToList();
            var lst=new List<dynamic>{
                new{Avatar="888",Money=250},
                new{Avatar="中国人民银行",Money=250},
                new{Product="999",Money=250},
                new{Avatar="333",Money=250},
            };
            _logger.LogInformation(lst.JsonSerialize());
            return lst;
        }


        [Route("{id}/friends")]
        public IEnumerable<dynamic> Index(Guid id)
        {
            var list=_userRes.Table.Select(x=>new{x.Id,x.Username,x.Password}).ToList();
            var lst=new List<dynamic>{
                new{Nickname="辣椒油",Fat=250},
                new{Nickname="罗伯特",Fat=250},
                new{Nickname="武状元",Fat=250},
                new{Nickname="哈哈哈哈",Fat=250},
            };
            return lst;
        }


        [Route("{id}/friends/{fid}")]
        public dynamic Index(Guid id,Guid fid)
        {
            var list=_userRes.Table.Select(x=>new{x.Id,x.Username,x.Password}).ToList();
            var lst=new{
                Code=1000,
                Msg="获取指定用户下的指定朋友成功",
                Data=new {id,fid}
            };
            return lst;
        }


        [HttpPost]
        public Users CreateUser(UserCto model)
        {
            System.Console.WriteLine(model);
            var user=new Users{
                Username=model.Username,
                Password=model.Password,
            };
            var list=_userRes.Add(user);
            return list;
        }


        [HttpPut]
        [Route("{id}")]
        public Users UpdateUsers(Guid id,UserCto model)
        {
            var user=_userRes.GetById(id);
            if(user!=null)
            {
                user.IsDeleted=model.IsDeleted;
                _userRes.Update(user);
            }
            return user;
        }


        [HttpDelete]
        [Route("{id}")]
        public dynamic DeleteUser(Guid id)
        {
            _userRes.Delete(id);
            return new{
                Code=1000,
                Msg="删除成功",
                Data=""
            };
        }

        public class UserCto
        {
            public string Username{get;set;}
            public string Password{get;set;}
            public bool IsActived{get;set;}
            public bool IsDeleted{get;set;}
        }
        

        // public int Index()
        // {
        //     // var id=Guid.NewGuid();
        //     // var xx=_userRes.Table.Where(x=>x.Username=="").ToArray();
        //     return 2;
        // }
        [Route("/test2")]
        public int main()
        {
            return 1;
        }
    }
}